import socket
from typing import Optional

def is_port_in_use(port: int) -> bool:
    """检查指定端口是否被占用"""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        try:
            s.bind(('localhost', port))
            return False
        except socket.error:
            return True

def find_available_port(start_port: int, max_attempts: int = 100) -> Optional[int]:
    """从指定端口开始查找可用端口"""
    current_port = start_port
    while current_port < start_port + max_attempts:
        if not is_port_in_use(current_port):
            return current_port
        current_port += 1
    return None 